package com.ljyh.forum.controller;

import com.ljyh.forum.entity.User;
import com.ljyh.forum.service.UserService;
import com.ljyh.forum.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@Tag(name = "认证接口", description = "用户注册和登录相关接口")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 注册请求体
     */
    public static class RegisterRequest {
        @NotBlank(message = "用户名不能为空")
        private String username;
        
        @NotBlank(message = "密码不能为空")
        private String password;
        
        @NotBlank(message = "昵称不能为空")
        private String nickname;

        // getters and setters
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public String getNickname() { return nickname; }
        public void setNickname(String nickname) { this.nickname = nickname; }
    }

    /**
     * 登录请求体
     */
    public static class LoginRequest {
        @NotBlank(message = "用户名不能为空")
        private String username;
        
        @NotBlank(message = "密码不能为空")
        private String password;

        // getters and setters
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

    /**
     * 注册响应体
     */
    public static class AuthResponse {
        private int code;
        private String msg;
        private Object data;

        public AuthResponse(int code, String msg, Object data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        // getters
        public int getCode() { return code; }
        public String getMsg() { return msg; }
        public Object getData() { return data; }
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public AuthResponse register(@Valid @RequestBody RegisterRequest request) {
        boolean success = userService.register(request.getUsername(), request.getPassword(), request.getNickname());
        if (success) {
            return new AuthResponse(200, "注册成功", null);
        } else {
            return new AuthResponse(400, "用户名已存在", null);
        }
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public AuthResponse login(@Valid @RequestBody LoginRequest request) {
        String token = userService.login(request.getUsername(), request.getPassword());
        if (token != null) {
            User user= userService.findByUsername(request.username);
            user.setPassword(null);
            return new AuthResponse(200, "登录成功", new TokenUserResponse(token, user));
        } else {
            return new AuthResponse(400, "用户名或密码错误", null);
        }
    }

    /**
     * Token响应体
     */
    public static class TokenUserResponse {
        private String token;
        private User user;

        public TokenUserResponse(String token, User user) {
            this.token = token;
            this.user=user;
        }


        public String getToken() { return token; }
        public User getUser() {return user;}
    }

    @GetMapping("/userinfo")
    @Operation(summary = "获取当前用户信息")
    public AuthResponse getUserInfo() {
        // 获取当前登录用户的用户名
        String currentUsername = SecurityUtils.getCurrentUsername();
        if (currentUsername == null) {
            return new AuthResponse(401, "用户未登录", null);
        }
        
        // 根据用户名查找用户信息
        User user = userService.findByUsername(currentUsername);
        if (user == null) {
            return new AuthResponse(404, "用户不存在", null);
        }
        
        // 移除敏感信息
        user.setPassword(null);
        
        return new AuthResponse(200, "获取用户信息成功", user);
    }
}
